home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 002 / interface2 / !DOCS / DEMO / C / INTERFACE next >
Encoding:
Text File  |  1991-08-24  |  20.6 KB  |  772 lines

  1. /*
  2.  *
  3.  *       Title                  : ADFS::$.c.interface
  4.  *       System                 : Risc-OS library
  5.  *       Version                : 1.2
  6.  *       Copyright              : ⌐ Software Interrupt
  7.  *       Date                   : 25nd September, 1990
  8.  *       Author                 : Simon Huntington
  9.  *
  10.  *       Function               : Demonstration of !Interface functions
  11.  *
  12.  *
  13.  *       Modification history.
  14.  *
  15.  *       Version                : Version 1 (August 1990)
  16.  *       Changes                : Total rewrite in C, many new features added to the module
  17.  *
  18.  */
  19.  
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "baricon.h"
  23. #include "dbox.h"
  24. #include "event.h"
  25. #include "interface.h"
  26. #include "menu.h"
  27. #include "os.h"
  28. #include "res.h"
  29. #include "resspr.h"
  30. #include "template.h"
  31. #include "visdelay.h"
  32. #include "werr.h"
  33. #include "wimp.h"
  34. #include "wimpt.h"
  35. #include "win.c"
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. /* 
  43.  *============================================================================
  44.  *
  45.  * Hash defines for the menu options
  46.  * 
  47.  *============================================================================
  48.  */
  49.  
  50. #define  iconbar_info           1
  51. #define  iconbar_operations     2
  52. #define  iconbar_quit           3
  53. #define  operations_boxes       1
  54. #define  operations_pointers    2
  55. #define  operations_misc        3
  56. #define  operations_help        4
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. /* 
  64.  *============================================================================
  65.  *
  66.  * Global variables
  67.  * 
  68.  *============================================================================
  69.  */
  70.  
  71. static menu            iconbar_menu;
  72. static menu            operation_menu;
  73. static dbox            info_dialogue;
  74. static wimp_w          main_window;
  75. static wimp_menustr   *window_menu;
  76. static int             interface_window              = 1;
  77. static int             interface_window_onscreen     = 0;
  78. static char           *current_status                = "Pointers, help switched off     ";
  79. static BOOL            pointer_direct                = FALSE;
  80. static BOOL            pointer_hand                  = FALSE;
  81. static BOOL            pointer_writable              = FALSE;
  82. static BOOL            pointer_menu                  = FALSE;
  83. static BOOL            help_status                   = TRUE;
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90. /* 
  91.  *============================================================================
  92.  *
  93.  * Function to open the about this program dialogue box
  94.  *
  95.  *    Parameters :     None
  96.  * 
  97.  *============================================================================
  98.  */
  99.  
  100. void info_about_program (void)
  101.      {          
  102.           if (info_dialogue = dbox_new ("prog_info"), info_dialogue != NULL)
  103.           {
  104.                dbox_show (info_dialogue);
  105.                dbox_fillin (info_dialogue);
  106.                dbox_dispose (&info_dialogue);
  107.           }
  108.      }
  109.  
  110.  
  111.  
  112.  
  113.  
  114. /* 
  115.  *============================================================================
  116.  *
  117.  * Function to border and change options of an icon
  118.  *
  119.  *    Parameters :     mouse - a mouse string containing icon, window, etc.
  120.  * 
  121.  *============================================================================
  122.  */
  123.  
  124. void workarea_button (mouse)
  125.  
  126.      wimp_mousestr mouse;
  127.      
  128.      {
  129.          if (mouse.bbits < 5)
  130.           wimpt_complain (wimp_bordericon (mouse));
  131.      }
  132.  
  133.  
  134.  
  135.  
  136.  
  137. /* 
  138.  *============================================================================
  139.  *
  140.  * Function to define a pointer
  141.  *
  142.  *    Parameters :     w_handle       - window handle
  143.  *                     x0             - workarea min x
  144.  *                     y0             - workarea min y
  145.  *                     x1             - workarea max x
  146.  *                     y1             - workarea max y
  147.  *                     pointer_string - string with pointer validation
  148.  * 
  149.  *============================================================================
  150.  */
  151.  
  152. wimp_pointer pointer_info (w_handle, x0, y0, x1, y1, pointer_string)
  153.  
  154.      int  w_handle,
  155.           x0,
  156.           y0,
  157.           x1,
  158.           y1;
  159.      char *pointer_string;
  160.  
  161.      {
  162.           wimp_pointer pointer;
  163.  
  164.           pointer.window_handle    = w_handle;
  165.           pointer.x0               = x0;
  166.           pointer.y0               = y0;
  167.           pointer.x1               = x1;
  168.           pointer.y1               = y1;
  169.           strcpy (pointer.ptr_validation, pointer_string);
  170.           
  171.           return pointer;
  172.      }
  173.  
  174.  
  175.  
  176.  
  177.  
  178. /* 
  179.  *============================================================================
  180.  *
  181.  * Function to switch pointers on and off
  182.  *
  183.  *    Parameters :     mouse - a mouse string containg window, icon, etc.
  184.  * 
  185.  *============================================================================
  186.  */
  187.      
  188. void pointer_handler (mouse)
  189.      
  190.      wimp_mousestr mouse;
  191.  
  192.      {
  193.  
  194.           if (mouse.bbits > 0 && mouse.bbits < 5)
  195.           {
  196.                wimp_pointer pointer_data;
  197.  
  198.                switch (mouse.i)
  199.                {
  200.                     case 2:
  201.                          pointer_data = pointer_info (mouse.w, 25, -80, 580, -228, "ptr_direct,13,7");
  202.  
  203.                          if (pointer_direct)
  204.                               wimpt_complain (wimp_releaseworkareapointer (pointer_data));
  205.                          else
  206.                               wimpt_complain (wimp_setworkareapointer (pointer_data));
  207.  
  208.                          pointer_direct = !pointer_direct;
  209.                          break;
  210.  
  211.                     case 3:
  212.                          pointer_data = pointer_info (mouse.w, 94, -80, 511, -193, "ptr_hand,10,9");
  213.  
  214.                          if (pointer_hand)
  215.                               wimpt_complain (wimp_releaseworkareapointer (pointer_data));
  216.                          else
  217.                               wimpt_complain (wimp_setworkareapointer (pointer_data));
  218.  
  219.                          pointer_hand = !pointer_hand;
  220.                          break;
  221.  
  222.                       case 4:
  223.                          pointer_data = pointer_info (mouse.w, 163, -80, 442, -165, "ptr_write,4,4");
  224.  
  225.                          if (pointer_writable)
  226.                               wimpt_complain (wimp_releaseworkareapointer (pointer_data));
  227.                          else
  228.                               wimpt_complain (wimp_setworkareapointer (pointer_data));
  229.  
  230.                          pointer_writable = !pointer_writable;
  231.                          break;
  232.  
  233.                     case 5:
  234.                          pointer_data = pointer_info (mouse.w, 232, -80, 373, -137, "ptr_menu,6,5");
  235.  
  236.                          if (pointer_menu)
  237.                               wimpt_complain (wimp_releaseworkareapointer (pointer_data));
  238.                          else
  239.                               wimpt_complain (wimp_setworkareapointer (pointer_data));
  240.  
  241.                          pointer_menu = !pointer_menu;
  242.                          break;
  243.  
  244.                     case 6:
  245.                          window_menu = (wimp_menustr *) menu_syshandle (iconbar_menu);
  246.                          wimpt_noerr (wimp_create_menu (window_menu, mouse.x - 64, mouse.y + 28));
  247.                          break;
  248.                }
  249.           }
  250.      }
  251.  
  252.  
  253.  
  254.  
  255.  
  256. /* 
  257.  *============================================================================
  258.  *
  259.  * Function to remove any pointers left once the window is deleted
  260.  *
  261.  *    Parameters :     None
  262.  * 
  263.  *============================================================================
  264.  */
  265.  
  266. void remove_any_pointers (void)
  267.      {
  268.      
  269.      wimp_pointer pointer_data;
  270.  
  271.  
  272.                pointer_data = pointer_info (main_window, 0, 0, 0, 0, "");
  273.                wimpt_complain (wimp_releaseworkareapointer (pointer_data));
  274.  
  275.                pointer_direct    = FALSE;
  276.                pointer_hand      = FALSE;
  277.                pointer_writable  = FALSE;
  278.                pointer_direct    = FALSE;
  279.      }
  280.  
  281.  
  282.  
  283.  
  284.  
  285. /* 
  286.  *============================================================================
  287.  *
  288.  * Function to close and delete the window and tidy up
  289.  *
  290.  *    Parameters :     window - window handle to delete 
  291.  * 
  292.  *============================================================================
  293.  */
  294.  
  295. void close_window (window)
  296.      
  297.      wimp_w window;
  298.  
  299.      {
  300.           wimpt_noerr (wimp_close_wind (window));
  301.  
  302.           if (interface_window_onscreen == 2)
  303.                remove_any_pointers ();
  304.  
  305.           wimpt_noerr (wimp_delete_wind (main_window));
  306.           win_claim_idle_events ((wimp_w) -1);
  307.           interface_window_onscreen = 0;
  308.      }
  309.  
  310.  
  311.  
  312.  
  313.  
  314. /* 
  315.  *============================================================================
  316.  *
  317.  * Function to handle button presses on the boxes window
  318.  *
  319.  *    Parameters :     mouse - a mouse string, which is used to determine
  320.  *                             what action to take
  321.  * 
  322.  *============================================================================
  323.  */
  324.  
  325. void boxes_handler (mouse)
  326.  
  327.      wimp_mousestr mouse;
  328.  
  329.      {
  330.           int time, sh = 0;
  331.           wimp_icon ic;
  332.  
  333.           switch (mouse.i)
  334.           {
  335.                case 10:
  336. while (wimp_get_icon_info (main_window, 1, &ic) == NULL);
  337. {
  338. sh += 1;
  339. }
  340. werr(0, "%d", sh);
  341.                     help_status = !help_status;
  342.                     menu_setflags (operation_menu, operations_help, help_status, 0);
  343.                     mouse.bbits = 0;
  344.                     workarea_button (mouse);
  345.                     break;
  346.  
  347.                case 11:
  348.                     {
  349.                          int button_state;
  350.  
  351.                          visdelay_begin ();
  352.  
  353.                          for (time = 0; time < 1000000; time ++)
  354.                               time = time;
  355.                          visdelay_end ();
  356.  
  357.                          button_state = mouse.bbits;
  358.                          mouse.bbits = 0;
  359.                          workarea_button (mouse);
  360.  
  361.                          if (button_state != 1)
  362.                               close_window (mouse.w);
  363.                     }
  364.                     break;              
  365.       
  366.                case 12:
  367.                     mouse.bbits = 0;
  368.                     workarea_button (mouse);
  369.                     close_window (mouse.w);
  370.                     break;
  371.           }
  372.      }
  373.  
  374.  
  375.  
  376.  
  377.  
  378. /* 
  379.  *============================================================================
  380.  *
  381.  * Function to handle any wimp poll functions to do with the main window
  382.  *
  383.  *    Parameters :     action - wimp poll action
  384.  *                     handle - null
  385.  * 
  386.  *============================================================================
  387.  */
  388.  
  389. typedef struct
  390. {
  391.     wimp_box   box;
  392.     wimp_box   parent;
  393. } wimp_idragstr;
  394.  
  395. static BOOL owns(wimp_eventstr *e, void *handle)
  396.  
  397. {
  398.   handle = handle ;
  399.   tracef1("xfersend raw event %i.\n", e->e);
  400.   switch (e->e)
  401.   { case wimp_EUSERDRAG:
  402.  werr (0, "dgf");
  403.         break;
  404.   }
  405. }
  406. void main_event_handler (action, handle)
  407.      
  408.      wimp_eventstr *action;
  409.      void *handle;
  410.      
  411.      {
  412.           wimpt_complain (wimp_pollpointer (action->e));
  413.  
  414.           switch (action->e)
  415.           {
  416.                case wimp_EOPEN:
  417.                     wimpt_noerr (wimp_open_wind (&action->data.o));
  418.                     break;
  419.  
  420.                case wimp_ECLOSE:
  421.                     close_window (action->data.o.w);
  422.                     break;
  423.  
  424.                case wimp_EBUT:
  425.                     if (action->data.but.m.bbits == wimp_BDRAGLEFT)
  426.                     {
  427.    wimp_idragstr dr;
  428.    wimp_wstate wstate;
  429.    wimp_icon icon;
  430.    os_regset    regs;
  431.    wimp_mousestr mouse_str;
  432.    int
  433.       x_limit = bbc_vduvar (bbc_XWindLimit) << bbc_vduvar (bbc_XEigFactor),
  434.       y_limit = bbc_vduvar (bbc_YWindLimit) << bbc_vduvar (bbc_YEigFactor),
  435.       screen_x0, screen_y0,
  436.       mouse_x, mouse_y,
  437.       x0, y0, x1, y1;
  438.  
  439.    wimp_get_point_info (&mouse_str);
  440.    mouse_x = mouse_str.x;
  441.    mouse_y = mouse_str.y;
  442.  
  443.    /*Find screen origin*/
  444.    wimp_get_wind_state (action->data.but.m.w, &wstate);
  445.    screen_x0 = wstate.o.box.x0 - wstate.o.x;
  446.    screen_y0 = wstate.o.box.y1 - wstate.o.y;
  447.  
  448.    /*Get initial icon position*/
  449.    wimp_get_icon_info (action->data.but.m.w, action->data.but.m.i, &icon);
  450.    x0 = screen_x0 + icon.box.x0;
  451.    y0 = screen_y0 + icon.box.y0;
  452.    x1 = screen_x0 + icon.box.x1;
  453.    y1 = screen_y0 + icon.box.y1;
  454.  
  455.    /*Set up drag*/
  456.    dr.box.x0    = x0;
  457.    dr.box.y0    = y0;
  458.    dr.box.x1    = x1;
  459.    dr.box.y1    = y1;
  460.    dr.parent.x0 = x0 - mouse_x; /*Expanded parent by box overlap*/
  461.    dr.parent.y0 = y0 - mouse_y;
  462.    dr.parent.x1 = x1 - mouse_x + x_limit;
  463.    dr.parent.y1 = y1 - mouse_y + y_limit;
  464.    regs.r[1] = (int) &dr;
  465.    os_swi (0x81689, ®s);
  466.  
  467.    win_add_unknown_event_processor (owns, NULL);
  468.  
  469.                     }
  470.                     if (action->data.but.m.bbits != 2 | interface_window_onscreen == operations_pointers)
  471.                     {                                                                                    
  472.                          workarea_button (action->data.but.m);
  473.  
  474.                          switch (interface_window_onscreen)
  475.                          {
  476.                               case operations_boxes:      
  477.                                    boxes_handler (action->data.but.m);
  478.                                    break;
  479.  
  480.                               case operations_pointers:
  481.                                    pointer_handler (action->data.but.m);
  482.                                    action->data.but.m.bbits = 0;
  483.                                    workarea_button (action->data.but.m);
  484.                                    break;         
  485.  
  486.                               case operations_misc:
  487.                                    action->data.but.m.bbits = 0;
  488.                                    workarea_button (action->data.but.m);
  489.                                    break;
  490.                          }
  491.                     }
  492.                     break;
  493.  
  494.                case wimp_ESEND:
  495.                case wimp_ESENDWANTACK:
  496.  
  497.                     switch (action->data.msg.hdr.action)
  498.                     {
  499.                          case wimp_MHELPREQUEST:
  500.  
  501.                               if (help_status == TRUE)
  502.                                   wimpt_noerr (wimp_sendhelp (&action->data.msg.hdr));
  503.  
  504.                               break;
  505.                     }
  506.                     break;                             
  507.           }
  508.      }
  509.  
  510.  
  511.  
  512.  
  513.  
  514. /* 
  515.  *============================================================================
  516.  *
  517.  * Function to create a window
  518.  *
  519.  *    Parameters :     name          - name of window to create
  520.  *                     window_handle - variable to place the created 
  521.  *                                     windows handle in
  522.  *
  523.  *    Returns    :     TRUE is successful
  524.  *
  525.  * 
  526.  *============================================================================
  527.  */
  528.  
  529. BOOL wimp_create_window (name, window_handle)
  530.      
  531.      char *name;
  532.      wimp_w *window_handle;
  533.  
  534.      {
  535.           wimp_wind *window;
  536.           
  537.           window = template_syshandle (name);
  538.           if (window == 0)
  539.              return FALSE;
  540.  
  541.           return (wimpt_complain (wimp_create_wind (window, window_handle)) == 0);          
  542.      }
  543.  
  544.  
  545.  
  546.  
  547.  
  548. /* 
  549.  *============================================================================
  550.  *
  551.  * Function to build a string containg current settings
  552.  *
  553.  *    Parameters :     None
  554.  * 
  555.  *============================================================================
  556.  */
  557.  
  558. void operation_menu_status (void)
  559.      {
  560.           switch (interface_window)
  561.           {
  562.                case 1:
  563.                     strcpy (current_status, "Boxes, ");
  564.                     break;
  565.  
  566.                case 2:
  567.                     strcpy (current_status, "Pointers, ");
  568.                     break;
  569.  
  570.                case 3:
  571.                     strcpy (current_status, "Miscellaneous, ");
  572.                     break;
  573.           }
  574.           
  575.           if (help_status == TRUE)
  576.                strcat (current_status, "help switched on");
  577.           else
  578.                strcat (current_status, "help switched off");
  579.  
  580.           if (interface_window_onscreen == 2)
  581.           {
  582.                wimp_icon  writable_icon;
  583.                
  584.                wimpt_noerr (wimp_get_icon_info (main_window, 7, &writable_icon));
  585.                strcpy (writable_icon.data.indirecttext.buffer, current_status);
  586.                wimpt_noerr (wimp_set_icon_state (main_window, 7, 0, 0));
  587.           }                                                             
  588.      }
  589.  
  590.  
  591.  
  592.  
  593.  
  594. /* 
  595.  *============================================================================
  596.  *
  597.  * Function called when a click on the icon is received
  598.  *
  599.  *    Parameters :     None
  600.  * 
  601.  *============================================================================
  602.  */
  603.  
  604. void iconbar_click (icon)
  605.  
  606.      wimp_i icon;
  607.  
  608.      {
  609.           if (interface_window_onscreen == TRUE)
  610.                werr (FALSE, "Only one window may be viewed");
  611.           else
  612.           {
  613.                wimp_wstate state;
  614.                char *window_design = "............";
  615.              
  616.                switch (interface_window)
  617.                {
  618.                     case operations_boxes:
  619.                          window_design = "boxes";
  620.                          break;
  621.  
  622.                     case operations_pointers:
  623.                          window_design = "pointers";
  624.                          break;         
  625.  
  626.                     case operations_misc:
  627.                          window_design = "misc";
  628.                          break;
  629.                }
  630.  
  631.                if (wimp_create_window (window_design, &main_window));
  632.                {
  633.                     win_register_event_handler (main_window, main_event_handler, 0);
  634.                     win_claim_idle_events (main_window);
  635.                     win_claim_unknown_events (main_window);
  636.                     interface_window_onscreen = interface_window;
  637.  
  638.                     if (interface_window == 2)
  639.                          operation_menu_status ();
  640.  
  641.                     if (wimpt_complain (wimp_get_wind_state (main_window, &state)) == 0);
  642.                     {
  643.                          state.o.behind = -1;
  644.                          wimpt_noerr (wimp_open_wind (&state.o));
  645.                     }
  646.                }
  647.           }
  648.      }
  649.  
  650.  
  651.  
  652.  
  653.  
  654. /* 
  655.  *============================================================================
  656.  *
  657.  * Function to handle selection on menu
  658.  *
  659.  *    Parameters :     handle - null
  660.  *                     hit    - menu entry selected
  661.  * 
  662.  *============================================================================
  663.  */
  664.  
  665. void iconbar_menu_selection (handle, hit)
  666.      
  667.      void *handle;
  668.      char *hit;
  669.  
  670.      {
  671.           switch (hit [0])
  672.           {
  673.                case iconbar_info:
  674.                     info_about_program ();
  675.                     break;
  676.                  
  677.                case iconbar_operations:
  678.                     switch (hit [1])
  679.                     {
  680.                          case operations_help:
  681.                               help_status = !help_status;
  682.                               menu_setflags (operation_menu, operations_help, help_status, 0);
  683.                               break;
  684.  
  685.                          case operations_boxes:   
  686.                          case operations_pointers:    
  687.                          case operations_misc:   
  688.                               menu_setflags (operation_menu, interface_window, 0, 0);
  689.                               menu_setflags (operation_menu, hit [1], 1, 0);
  690.                               interface_window = hit [1];
  691.                               break;
  692.                     }
  693.                     operation_menu_status ();
  694.                     break;
  695.  
  696.                case iconbar_quit:
  697.                     exit (0);
  698.           }
  699.      }
  700.  
  701.  
  702.  
  703.  
  704.  
  705. /* 
  706.  *============================================================================
  707.  *
  708.  * Function to initialise application
  709.  *
  710.  *    Parameters :     None
  711.  *
  712.  *    Returns :        TRUE if all went well                    
  713.  * 
  714.  *============================================================================
  715.  */
  716.  
  717. BOOL initialise_application (void)
  718.      {
  719.           wimpt_init ("Interface");
  720.           res_init ("Interface");
  721.           resspr_init ();
  722.           template_init ();
  723.           dbox_init ();
  724.           visdelay_init ();
  725.  
  726.           baricon ("!Interface", (int) resspr_area (), iconbar_click);
  727.           iconbar_menu = menu_new ("Interface", ">Info, Operations, Quit");
  728.           operation_menu = menu_new ("Operations", "!Boxes, Pointers, Misc|!Help");
  729.           menu_submenu (iconbar_menu, 2, operation_menu);
  730.  
  731.           if (! event_attachmenu (win_ICONBAR, iconbar_menu, iconbar_menu_selection, 0))
  732.                return FALSE;
  733.    
  734.           wimpt_complain (wimp_claiminterface ());
  735.           return TRUE;
  736.      }
  737.  
  738.  
  739.  
  740.  
  741.  
  742. /* 
  743.  *============================================================================
  744.  *
  745.  * Main control of the program
  746.  *
  747.  *    Parameters :     None
  748.  *
  749.  *============================================================================
  750.  */
  751.  
  752. int main (void)
  753.      {
  754.           if (atexit ((void *) wimp_releaseinterface))
  755.                return 0;
  756.  
  757.           if (! initialise_application ())
  758.                return 0;
  759.  
  760.           event_setmask(event_getmask() & ~wimp_EMNULL);
  761.  
  762.           while (TRUE)
  763.                event_process ();
  764.  
  765.           return 0;
  766.      }
  767.          
  768.  
  769.  
  770.  
  771.  
  772.